#include "aw9523b.h"
#include "polsys.h"

// AW9523B寄存器地址定义

// IO输入值，只读，反映管脚电平
#define AW9523B_REG_INPUT_P0		0X00
#define AW9523B_REG_INPUT_P1		0X01

// IO输出值，对于输入模式的IO口值无效，初始值决定于AD0与AD1
#define AW9523B_REG_OUTPUT_P0	    0X02
#define AW9523B_REG_OUTPUT_P1	    0X03

// 方向选择，0-输出【默认】，1-输入。
#define AW9523B_REG_DIR_P0	        0X04
#define AW9523B_REG_DIR_P1	        0X05

// 电平变化中断使能，0-中断使能【默认】，1-中断不使能.
// 使能后将通过芯片INT管脚发送中断信号（低电平）
#define AW9523B_REG_INT_P0		    0X06
#define AW9523B_REG_INT_P1		    0X07

// 寄存器的值固定为0X23，只读
#define AW9523B_REG_ID				0X10

// 全局控制寄存器，默认0X00
// bit4: 0-P0口为Open-Drain模式，1-P0口为Push-Pull模式。
// bit1~0: 256步调光最大电流选择，0-37mA,1-27mA,2-18mA,3-9mA.
// 其它位无意义
#define AW9523B_REG_CTL				0X11

// 工作模式选择，0-LED恒流模式，1-GPIO模式【默认】。
#define AW9523B_REG_LED_MODE_P0		0X12
#define AW9523B_REG_LED_MODE_P1		0X13

// 256步调光寄存器，LED模式下有效，调节驱动电流。
#define AW9523B_REG_DIM_P10			0X20
#define AW9523B_REG_DIM_P11			0X21
#define AW9523B_REG_DIM_P12			0X22
#define AW9523B_REG_DIM_P13			0X23
#define AW9523B_REG_DIM_P00			0X24
#define AW9523B_REG_DIM_P01			0X25
#define AW9523B_REG_DIM_P02			0X26
#define AW9523B_REG_DIM_P03			0X27
#define AW9523B_REG_DIM_P04			0X28
#define AW9523B_REG_DIM_P05			0X29
#define AW9523B_REG_DIM_P06			0X2A
#define AW9523B_REG_DIM_P07			0X2B
#define AW9523B_REG_DIM_P14			0X2C
#define AW9523B_REG_DIM_P15			0X2D
#define AW9523B_REG_DIM_P16			0X2E
#define AW9523B_REG_DIM_P17			0X2F

// 软复位寄存器，写入0X00将触发复位
#define AW9523B_REG_SW_RST			0X7F


static u8 mP0ODR;
static u8 mP1ODR;

void AW9523B_Init()
{
	u8 i;

	_AW9523B_IICInit();
	_AW9523B_SetRstLevel(1);
	Delay_ms(25);
	_AW9523B_SetRstLevel(0);
	Delay_ms(50);
	_AW9523B_SetRstLevel(1);
	Delay_ms(50);
	_AW9523B_Write(AW9523B_REG_SW_RST,0X00);	//软复位
	Delay_ms(50);

	_AW9523B_Write(AW9523B_REG_CTL,
		  (_AW9523B_P0_GPIO_PP << 4)
		| (_AW9523B_I_MAX << 0));
	_AW9523B_Write(AW9523B_REG_LED_MODE_P0, _AW9523B_P0_MODE_DEFAULT);
	_AW9523B_Write(AW9523B_REG_LED_MODE_P1, _AW9523B_P1_MODE_DEFAULT);
	AW9523B_SetP0(_AW9523B_P0_ODR_DEFAULT);
	AW9523B_SetP1(_AW9523B_P1_ODR_DEFAULT);
	AW9523B_SetP0Dir(_AW9523B_P0_DIR_DEFAULT);
	AW9523B_SetP1Dir(_AW9523B_P1_DIR_DEFAULT);
	AW9523B_SetP0Inte(_AW9523B_P0_INTE_DEFAULT);
	AW9523B_SetP1Inte(_AW9523B_P1_INTE_DEFAULT);
	for(i=AW9523B_LED_PORT_P10; i<=AW9523B_LED_PORT_P17; i++)
	{
		AW9523B_SetLEDBrightness((aw9523b_led_port_t)i, 255);
	}
	_AW9523B_Read(AW9523B_REG_INPUT_P0);
	_AW9523B_Read(AW9523B_REG_INPUT_P1);
}

IN_LINE u8 AW9523B_GetP0()
{
	return _AW9523B_Read(AW9523B_REG_INPUT_P0);
}

IN_LINE u8 AW9523B_GetP1()
{
	return _AW9523B_Read(AW9523B_REG_INPUT_P1);
}

IN_LINE bool AW9523B_GetP0x(u8 pin_bit)
{
	return ((_AW9523B_Read(AW9523B_REG_INPUT_P0) >> pin_bit) & 0xFE);
}

IN_LINE bool AW9523B_GetP1x(u8 pin_bit)
{
	return ((_AW9523B_Read(AW9523B_REG_INPUT_P1) >> pin_bit) & 0xFE);
}

IN_LINE void AW9523B_SetP0(u8 new_data)
{
	mP0ODR = new_data;
	_AW9523B_Write(AW9523B_REG_OUTPUT_P0, new_data);
}

IN_LINE void AW9523B_SetP1(u8 new_data)
{
	mP1ODR = new_data;
	_AW9523B_Write(AW9523B_REG_OUTPUT_P1, new_data);
}

void AW9523B_SetP0x(u8 pin_bit, bool level)
{
	if(level)
		mP0ODR |= (1<<pin_bit);
	else
		mP0ODR &= ~(1<<pin_bit);
	_AW9523B_Write(AW9523B_REG_OUTPUT_P0, mP0ODR);
}

void AW9523B_SetP1x(u8 pin_bit, bool level)
{
	if(level)
		mP1ODR |= (1<<pin_bit);
	else
		mP1ODR &= ~(1<<pin_bit);
	_AW9523B_Write(AW9523B_REG_OUTPUT_P1, mP1ODR);
}

IN_LINE void AW9523B_SetP0Dir(u8 dir)
{
	_AW9523B_Write(AW9523B_REG_DIR_P0, dir);
}

IN_LINE void AW9523B_SetP1Dir(u8 dir)
{
	_AW9523B_Write(AW9523B_REG_DIR_P1, dir);
}

IN_LINE void AW9523B_SetP0Inte(u8 inte)
{
	_AW9523B_Write(AW9523B_REG_INT_P0, inte);
}

IN_LINE void AW9523B_SetP1Inte(u8 inte)
{
	_AW9523B_Write(AW9523B_REG_INT_P1, inte);
}

IN_LINE void AW9523B_SetLEDBrightness(aw9523b_led_port_t port, u8 brightness)
{
	_AW9523B_Write(port, brightness);
}













